/**************************************************************************
 * @file usr_cfg.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-10-21
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/

 #ifndef USR_CFG__H_H
 #define USR_CFG__H_H
 
 #ifdef __cplusplus
 extern "C"{
 #endif
 #include<stdint.h>
 #include<stdio.h>
 
 
 #include <string.h>

#include "hc32_ll.h"
#include "ev_hc32f334_lqfp64_bsp.h"
#include "usr_buck_boost_ctrl.h"

#define FW_VERSION "V1.3"
#define LCD_PANEL_ENABLE 0

#define BUCK_BOOST_CC_FUNCTION_ENABLE 0

#define SWINT_CH            (SWINT_CH31)
#define SWINT_PRIO          (DDL_IRQ_PRIO_DEFAULT)

typedef enum {

    CFG_MENU_NULL=0,
    CFG_VOLTAGE_ACTIVE,
    CFG_CURRENT_ACTIVE,

    CFG_MENU_MAX
}CfgMenuActiveT;


#define CFG_SAVE_ADDR    0x00
#define CFG_SAVE_TIMEOUT 3000u  /*mS*/
#define CFG_VO_MAX       36000u /*mV*/
#define CFG_VO_MIN       400u   /*mV*/
#define CFG_VO_DEFAULT   2500u

#define CFG_IO_MAX     5000u /*mA*/
#define CFG_IO_MIN     100u  /*mA*/
#define CFG_IO_DEFAULT 500u  /*mA*/

typedef enum {

    PS_OUT_DISABLE=0,
    PS_OUT_ENABLE,

}PS_OUT_CTRL_T;
typedef struct buck_boost_err_infor {
    uint8_t input_uvp_err; /*输入欠压保护*/
    uint8_t input_ovp_err; /*输入过压保护*/

    uint8_t output_ovp_err; /*输出过压保护*/
    uint8_t output_ocp_err; /*输出过流保护*/

} BuckBoostErrInfor;

typedef struct usr_cfg_dats{
    int32_t cfg_vo;
    int32_t cfg_io;
}UsrCfgDatsT;
typedef enum {
    CV_ACTIVE=0,
    CC_ACTIVE,
}CV_CC_FLAGE_T;

typedef struct main_dat {
    uint8_t rx_cnt;
    uint8_t rx_dat_buf[32];
    uint32_t usr_tick;
    UsrBuckBoostDatT buck_boost;

    uint16_t lcd_flush_cnt;
    uint8_t lcd_flush_active;
    CfgMenuActiveT cfg_state;
    uint16_t cfg_active_timeout;

    UsrCfgDatsT usr_cfg;
    uint32_t usr_cfg_save_timeout;
    float soft_vo;
    uint8_t soft_up_en;
    uint8_t soft_up_cnt;
    PS_OUT_CTRL_T ps_out;
    int16_t ps_delay_to_close_hrpwm;

    CV_CC_FLAGE_T cv_cc_flg;
    BuckBoostErrInfor err_infor;
    uint8_t uvp_flg;  // 输入欠压保护标志位 0:没有发生了欠压，1:欠压或者电压源带载能力不足导致
    uint8_t pd_state;
    uint8_t pd_ver;

    uint8_t ovcp_flg; //过流保护标志位

} UsrMainDatT;

/* 宏定义：指定函数到.ramfunc段 */
#define RAM_FUNC __RAM_FUNC //__attribute__((section(".ramfunc"), noinline))
// #define RAM_FUNC 
extern UsrMainDatT main_dat;

#define VIN_ADC_MAX_BUF 8
#define OVER_TEMPERATURE_PROTECT_THRESOLD 60 /*C degree*/

#define CAP_DISCARGE_ON  1
#define CAP_DISCARGE_OFF 0
#define CAP_DISCARGE_CTL_PORT (GPIO_PORT_A)
#define CAP_DISCARGE_CTL_PIN  (GPIO_PIN_12)
#define CAP_DISCARGE_CTL(level)  ((level) ? GPIO_SetPins(CAP_DISCARGE_CTL_PORT, CAP_DISCARGE_CTL_PIN) : GPIO_ResetPins(CAP_DISCARGE_CTL_PORT, CAP_DISCARGE_CTL_PIN))
 
 void buck_boost_data_init(void);

 #ifdef __cplusplus
 }
 #endif
 
 #endif //USR_CFG__H_H
 
 
 